//
//  VideoCommentDetailController.swift
//  maopu
//
//  Created by 黄海波 on 2017/4/20.
//  Copyright © 2017年 maopu. All rights reserved.
// 视频评论详情页

import UIKit

class VideoCommentDetailController: BaseViewController , SortViewDelegate{
    fileprivate var tableView:UITableView!
    fileprivate var bottomView:SendCommentView!
    fileprivate var sortBtn:UIButton!
    fileprivate var sortView:SortView!
    fileprivate var moreToolView:MoreToolView!

    override func viewDidLoad() {
        super.viewDidLoad()

        self.initView()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
    
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        NotificationCenter.default.removeObserver(self.bottomView)
    }
    
    fileprivate func initView(){
        self.automaticallyAdjustsScrollViewInsets = false
        self.initNavigationView()
        
        self.tableView = UITableView.init(frame: CGRect.init(x: 0, y: 64, width: AppWidth, height: AppHeight - 64 - 50), style:UITableViewStyle.grouped)
        self.tableView.backgroundColor = UIColor.m_viewBackgroundColor()
        self.tableView.estimatedRowHeight = 300
        self.tableView.rowHeight = UITableViewAutomaticDimension
        self.tableView.separatorStyle = .none
        self.tableView.keyboardDismissMode = .onDrag
        self.tableView.tableHeaderView = UIView.init(frame: CGRect.init(x: 0, y: 0, width: AppWidth, height: 0.01))
        self.tableView.tableFooterView = UIView.init(frame: CGRect.init(x: 0, y: 0, width: AppWidth, height: 0.01))
        self.tableView.delegate = self
        self.tableView.dataSource = self
        self.tableView.register(UINib(nibName: "ReplyItemCell", bundle: nil), forCellReuseIdentifier: "ReplyItemCell")
        self.view.addSubview(self.tableView)
        
        self.bottomView = SendCommentView.init(frame: CGRect.init(x: 0, y: AppHeight - 50 , width: AppWidth, height: 50))
        self.view.addSubview(self.bottomView)
    }
    
    fileprivate func initNavigationView(){
        self.navigationItem.title = "评论详情"
        self.navigationItem.leftBarButtonItem = UIBarButtonItem.setBackBarButton(action: #selector(self.goBack), target: self)
        
        sortBtn = UIButton.init(type: .custom)
        sortBtn.frame = CGRect.init(x: 0, y: 0, width: 60, height: 15)
        sortBtn.set(image: UIImage(named: "sortUp_ico"), title: "正序", titlePosition: .right, additionalSpacing: 4, state: UIControlState())
        sortBtn.setTitleColor(UIColor.m_170Color(), for: UIControlState())
        sortBtn.titleLabel?.font = UIFont.LightDefaultFont(12)
        sortBtn.addTarget(self, action: #selector(self.showSortView), for: .touchUpInside)
        let sortBarBtn = UIBarButtonItem.init(customView: sortBtn)
        
        let moreBtn = UIButton.init(type: .custom)
        moreBtn.frame = CGRect.init(x: 0, y: 0, width: 16, height: 4)
        moreBtn.setImage(UIImage.init(named: "more_ico"), for: UIControlState())
        moreBtn.addTarget(self, action: #selector(self.showShareView), for: .touchUpInside)
        let moreBarBtn = UIBarButtonItem.init(customView: moreBtn)
        self.navigationItem.rightBarButtonItems = [moreBarBtn , sortBarBtn]
    }
    
    //MARK:查看更多
    @objc fileprivate func showSortView(){
        Log.shared.printLog("显示排序")
        if self.sortView == nil{
            self.sortView = SortView.init(type: 1)
            self.sortView.delegate = self
        }
        self.sortView.show()
    }
    //MARK:只看楼主
    @objc fileprivate func showShareView(){
        if self.moreToolView == nil{
            self.moreToolView = MoreToolView.init(isVideo: true, onlyShowShare: false, onlyShowReport: true)
        }
        self.moreToolView.show()
    }
    
    func sortView(sortView: SortView, didSelectRowAt indexPath: Int , title:String) {
        sortBtn.set(image: UIImage(named: "sortUp_ico"), title: title, titlePosition: .right, additionalSpacing: 4, state: UIControlState())
    }
}

extension VideoCommentDetailController:UITableViewDelegate , UITableViewDataSource{
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 3
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "ReplyItemCell", for: indexPath) as! ReplyItemCell
        cell.selectionStyle = .none
        cell.refreshCellData(indexPathRow: indexPath.row, cellType: VideoReplyCellType.comment)
        return cell
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
    }
    
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 0.01
    }
    
    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return 0.1
    }
}
